package com.shiguo365.market.presenter.implement;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.shiguo365.market.display.view.IRegisterView;
import com.shiguo365.market.presenter.contract.IRegisterPresenter;

import biz.yfsoft.app.aengine.api.AEClient;
import biz.yfsoft.app.aengine.api.AEParam;
import biz.yfsoft.app.aengine.api.BaseAECallback;

/**
 * Created by james on 16/7/28.
 */
public class RegisterPresenter implements IRegisterPresenter {

    private static final String TAG = "RegisterPresenter";
    private IRegisterView iRegisterView;
    private Activity activity;

    public RegisterPresenter(@NonNull Activity activity,@NonNull IRegisterView iRegisterView){

    }

    @Override
    public void register(String username, String code, String pwd, String shopName, String realName, String province, String city, String street) {
        AEClient _client = new AEClient("shiguo.user.register");
        AEParam aeParam =new AEParam();

        aeParam.set("username",username);           //手机号
        aeParam.set("code",code);                   //短信验证码
        aeParam.set("password",pwd);                //登录密码
        aeParam.set("shopName",shopName);           //公司名称
        aeParam.set("realName",realName);           //您的姓名
        aeParam.set("province",province);           //省份
        aeParam.set("city",city);                   //城市
        aeParam.set("street",street);               //公司地址
        aeParam.set("source",2);                    //1；微信，2：android 3：ios
        _client.call(aeParam,new BaseAECallback() {

            @Override
            public void onSuccess(JSONObject a) {
                Log.d(TAG,a.toJSONString());
                iRegisterView.ShowRegisterMessages("恭喜您注册成功,系统正在为你服务");
                iRegisterView.RegisterSuccess();
            }

            @Override
            public void onError( String errno, String message) {
                Log.e(TAG,"onFailure ......." + errno + ";message:" + message);
                iRegisterView.ShowRegisterMessages("对不起,注册失败,失败原因:"+message);
            }

            @Override
            public void onFinally() {
                //mView.onGetUserInfoFinished();
                Log.d(TAG,"zzzzz");
            }
        });
    }

    /**
     * 获取六位随机码
     * @param mobile
     * @auto James 2016-07-28
     */
    @Override
    public void smscheck(String mobile) {
        AEClient _client = new AEClient("shiguo.user.juheSMS");
        AEParam aeParam =new AEParam();

        String tpl_id = "17582";
        aeParam.set("mobile",mobile);
        aeParam.set("tpl_id",tpl_id);    //短信模版id
        aeParam.set("source",1);         //1：注册，2：找回密码
        _client.call(aeParam,new BaseAECallback() {

            @Override
            public void onSuccess(JSONObject a) {
                Log.d(TAG,a.toJSONString());
                iRegisterView.ShowRegisterMessages("发送成功,请注意查收");

            }

            @Override
            public void onError( String errno, String message) {
                Log.e(TAG,"onFailure ......." + errno + ";message:" + message);
                iRegisterView.ShowRegisterMessages("短信接口未收到请求,请再尝试发送请求");
            }

            @Override
            public void onFinally() {
                //mView.onGetUserInfoFinished();
                Log.d(TAG,"zzzzz");
            }
        });
    }
}
